package umich.max.geolocation.util;

import java.util.HashSet;
import java.util.Set;

import max.nlp.dal.blog.blogger.BlogAuthorProfile;
import max.nlp.dal.blog.blogger.BloggerBlog;
import max.nlp.dal.blog.blogger.BloggerDB;
import max.nlp.dal.blog.blogger.BloggerPost;
import max.nlp.dal.blog.blogger.ParsedLocation;

import com.mongodb.DBCursor;

public class BlogUtils {

	public static boolean isBlogFromCity(BloggerBlog b, String city) {
		BloggerDB db = BloggerDB.getInstance();
		BlogAuthorProfile profile = db.findProfileForBlog(b);
		ParsedLocation loc = profile.getParsedLocation();
		if (loc == null)
			return false;
		else {
			String c = loc.getCity();
			if (c == null) {
				return false;
			} else {
				if (c.equals(city)) {
					return true;
				} else {
					return false;
				}
			}
		}
	}

	public static boolean isBlogFromState(BloggerBlog b, String state) {
		BloggerDB db = BloggerDB.getInstance();
		BlogAuthorProfile profile = db.findProfileForBlog(b);
		ParsedLocation loc = profile.getParsedLocation();
		if (loc == null)
			return false;
		else {
			if (loc.getState().equals(state))
				return true;
			else
				return false;
		}
	}

	public static boolean isBlogNotFromState(BloggerBlog b, String state) {
		if (!state.equals("ANY"))
			return isBlogFromState(b, state);
		else {
			BloggerDB db = BloggerDB.getInstance();
			BlogAuthorProfile profile = db.findProfileForBlog(b);
			ParsedLocation loc = profile.getParsedLocation();
			if (loc == null)
				return true;
			else {
				if (loc.getState().equals(state))
					return false;
				else
					return true;
			}
		}
	}

	public static Set<String> getAllStates() {
		HashSet<String> states = new HashSet<String>();
		BloggerDB db = BloggerDB.getInstance();
		DBCursor itr = db.getBlogIterator();

		while (itr.hasNext()) {
			BloggerBlog b = new BloggerBlog(itr.next());
			BlogAuthorProfile pro = db.findProfileForBlog(b);
			ParsedLocation pl = pro.getParsedLocation();
			if (pl != null) {
				String state = pl.getState();
				states.add(state);
			}
		}
		return states;
	}
	
	public static boolean blogHasAtLeastOneLocation(BloggerBlog b){
		for ( BloggerPost  post : b.getPosts()){
			if (post.getLocations().size() > 0)
				return true;
		}
		return false;
	}

	public static void main(String[] args) { //416 from ny
		String state = "NY";
		BloggerDB db = BloggerDB.getInstance();
		DBCursor itr = db.getBlogIterator();
		int y = 0;
		int n = 0;
		while (itr.hasNext()) {
			BloggerBlog b = new BloggerBlog(itr.next());
			boolean fromState = isBlogNotFromState(b, state);
			if (fromState)
				y++;
			else
				n++;
		}
		System.out.println(y);
		System.out.println(n);
	}
}
